<?php

function echoTeamFixtureRow($fixture, $rounds, & $row, $teamId) {
    echo getEchoTR($row++, "center");
    $name = $fixture->round;
    if (isset ($rounds) && isset ($rounds[$fixture->round])) {
        $round = $rounds[$fixture->round];
        $name = $round->name;
    }
    echo "  <td>" . $fixture->season->season . "</td>\n";
    echo "  <td>$name</td>\n";
    if ($fixture->homeTeam->id == $teamId) {
	    echo "  <td>" . getEchoValue($fixture->awayTeam->coach->getName()) . "</td>\n";
	    echo "  <td>" . getEchoValue($fixture->awayTeam->getRosterLink()) . "</td>\n";
	    echo "  <td>" . getEchoValue($fixture->awayTeam->race->name) . "</td>\n";
	    echo "  <td>" . getEchoValue($fixture->awayTeam->getTR()) . "</td>\n";
    } else {
	    echo "  <td>" . getEchoValue($fixture->homeTeam->coach->getName()) . "</td>\n";
	    echo "  <td>" . getEchoValue($fixture->homeTeam->getRosterLink()) . "</td>\n";
	    echo "  <td>" . getEchoValue($fixture->homeTeam->race->name) . "</td>\n";
	    echo "  <td>" . getEchoValue($fixture->homeTeam->getTR()) . "</td>\n";
    }
    echo "</tr>\n";
}


function echoMatch($myRecord, $opponentRecord, $match) {
    global $teams;
    $ad = new ApprovalDao();
    $hasApprovals = $ad->findOutstandingMatchApprovals($match);
    if ($hasApprovals) {
        $hasApprovals = count($hasApprovals) > 0;
    }
    
    $opponent = $teams->getTeam($opponentRecord->teamId);
    echo "<tr align=\"middle\">\n<td>";
    if ($myRecord->score > $opponentRecord->score) {
        echo "Won";
    } else
        if ($myRecord->score == $opponentRecord->score) {
            echo "Drawn";
        } else {
            echo "Lost";
        }
    echo "</td>\n<td>" . $opponent->coach->getName() . "</td>\n";
    echo "<td>" . $opponent->getRosterLink() . " (" . $opponent->race->name . ")</td>\n";
    echo "<td>" . $myRecord->score . "</td>\n";
    echo "<td>" . $opponentRecord->score . "</td>\n";
    echo "<td>" . $myRecord->casualties . "</td>\n";
    echo "<td>" . $opponentRecord->casualties . "</td>\n";
    echo "<td>" . $myRecord->winnings . "</td>\n";
    echo "<td>" . getFanText($myRecord->fanFactor) . "</td>\n";
    echo "<td align=center><a href=\"match_view.php?matchId=" . $match->id . "\">Details";
    if ($hasApprovals) {
        echo "<br />Unapproved";
    }
	echo "</a></td>\n</tr>\n";
}

function echoStat($stat, $positionStat) {
    if ($stat == $positionStat) {
        echo "  <td>" . getEchoValue($stat) . "</td>\n";
    } else {
        echo "  <td><b>" . getEchoValue($stat) . "</b></td>\n";
    }
}

function echoPlayerRow($player, $id, $position, $usesExps) {

    if (!$player) {
        echo "<tr align=center>\n  <td>" . $id . "</td>\n  <td align=left>&nbsp;</td>\n  <td align=left>&nbsp;</td>\n  <td>&nbsp;</td>\n  <td>&nbsp;</td>\n  <td>&nbsp;</td>\n  <td>&nbsp;</td>\n  <td align=left>&nbsp;</td>\n  <td>&nbsp;</td>\n  <td>&nbsp;</td>\n  <td>&nbsp;</td>\n  <td>&nbsp;</td>\n  <td>&nbsp;</td>\n  <td>&nbsp;</td>\n  <td>&nbsp;</td>\n  <td align=\"right\">&nbsp;</td>\n</tr>\n";
    } else {
        echo "<tr align=center>\n";
        echo "  <td>$player->link" . $id . "</a></td>\n";
        echo "  <td align=left class=\"small\">$player->link" . getEchoValueShrink($player->name, 15) . "</a></td>\n";
        echo "  <td align=left class=\"small\">" . getEchoValue($player->position) . "</td>\n";
        if (!$position) {
            echo "  <td>" . getEchoValue($player->ma) . "</td>\n";
            echo "  <td>" . getEchoValue($player->st) . "</td>\n";
            echo "  <td>" . getEchoValue($player->ag) . "</td>\n";
            echo "  <td>" . getEchoValue($player->av) . "</td>\n";
        } else {
            echoStat($player->ma, $position->ma);
            echoStat($player->st, $position->st);
            echoStat($player->ag, $position->ag);
            echoStat($player->av, $position->av);
        }
        echo "  <td align=left class=\"small\">" . getEchoValueShrink($player->skills, 20) . "</td>\n";
        echo "  <td>" . getEchoValue($player->getInjuries()) . "</td>\n";
        echo "  <td>" . getEchoValue($player->completions) . "</td>\n";
        echo "  <td>" . getEchoValue($player->tds) . "</td>\n";
        echo "  <td>" . getEchoValue($player->interceptions) . "</td>\n";
        echo "  <td>" . getEchoValue($player->casualties) . "</td>\n";
        if ($usesExps) {
            echo "  <td>" . getEchoValue($player->exps) . "</td>\n";
        } else {
            echo "  <td>" . getEchoValue($player->mvps) . "</td>\n";
        }
        echo "  <td>" . getEchoValue($player->getSPPS()) . "</td>\n";
        echo "  <td align=\"right\">" . getEchoValue($player->cost) . "k</td>\n";
        echo "</tr>\n";
    }
}
include ("includes/init.php");

$id = getParam("id");
$team = $teams->getTeam($id);
$title = $team->name;
$wonSeasons = $sd->findWonSeasons($id);
$canEdit = isset ($loggedin) and $loggedin->canUpdateTeam($team); 
$submitComment = getParam("submitComment");
if ($canEdit && isset($submitComment)) {
	$comments = getParam("comments");
    $team->comments = stripslashes($comments);
    $team->save();
    $teams->setTeam($team);
    $_SESSION["teams"]=$teams;
}

echo<<< EOQ
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>$title</title>
    <meta http-equiv=Content-Type content="text/html; charset=windows-1252">
    <meta name="description" content="$defaultPageTitle">
    <meta name="keywords" content="bloodbowl, blood bowl, league, games workshop, games, bbl, blood, bowl, $keywords">
    <meta http-equiv="Cache-Control" content="max-age=0">
    <meta http-equiv="Pragma" content="no-cache">
    <link href="$rootDir/images/favicon.ico" rel="icon">
    <link href="$rootDir/standard.css" rel="stylesheet">
	<script language=javascript type='text/javascript'>
	function changeDivDisplay(id, style) {
		if (document.getElementById) { // DOM3 = IE5, NS6
			document.getElementById(id).style.display = style;
		} else if (document.layers) { // Netscape 4
	        var exeLine='document.' + id + '.display = '.style;
	        eval (exeLine);
		} else { // IE 4
	        var exeLine='document.all.' + id + '.display = '.style;
	        eval (exeLine);
		}
	}
	</script> 
  </head>
  <body bgColor="$team->background" text="$team->text" vLink="$team->text" link="$team->text">
    <font face="MS Sans Serif" size="4">
    <center>
		<table border="0" width="100%">
			<tr>
EOQ;
	foreach($wonSeasons as $wonSeason) {
	    if (strlen($wonSeason->image) > 0) {
echo<<< EOQ
				<td align="center" width="35" ><img src="$wonSeason->image" TITLE="Winner $wonSeason->season :- $wonSeason->imageText" BORDER="0"></td>
EOQ;
	    } else {
echo<<< EOQ
				<td align="center"><div class="sup"><span title="Winner $wonSeason->season :- $wonSeason->imageText">Winner<br />$wonSeason->season</span</div></td>
EOQ;
	    } 
	}				
echo<<< EOQ
				<td align="center" valign="center" width="*%"><h1>$team->name</h1></td>
EOQ;
if ($canEdit) {
    echo<<< EOQ
				<td align="center" width="35" ><a href="team_editor.php?id=$team->id"><img src="$rootDir/images/edit.jpg" title="Edit Team"/></a></td>
EOQ;
}
	$anchor = $team->id;
	if (isset($team->conference)) {
		$anchor = urlencode($team->conference);
	} 
echo<<< EOQ
				<td align="center" width="35"><a href="league.php#$anchor"><img src="$rootDir/images/tables.jpg" title="League Tables"/></a></td>
				<td align="center" width="35"><a href="team_stats.php?id=$id"><img src="$rootDir/images/stats.jpg" title="Team Statistics"/></a></td>
				<td align="center" width="35"><a href="team_deaths.php?id=$id"><img src="$rootDir/images/skull-and-crossbones.png" title="Dead and Retired Players"/></a></td>
				<td align="center" width="35"><a href="pf_team_viewer.php?id=$id" target="dsl_pf"><img src="$rootDir/images/printer.gif" title="Printer Friendly" /></a></td>
			</tr>
		</table>    
        <table border="1" width="100%" borderColorLight=$team->borderLight borderColorDark=$team->borderDark>
          <tr style="background-color: $team->titleBackground; color: $team->titleText;">
            <th><span title="Roster Number">#</span></th>
            <th>Player Name</th>
            <th>Position</th>
            <th><span title="Movement">ma</span></th>
            <th><span title="Strength">st</span></th>
            <th><span title="Agility">ag</span></th>
            <th><span title="Armour">av</span></th>
            <th><span title="Skills and other">Skills</span></th>
            <th><span title="Injuries - MNG and Niggles">inj</span></th>
            <th><span title="Completions">cp</span></th>
            <th><span title="Touchdowns">td</span></th>
            <th><span title="Interceptions">in</span></th>
            <th><span title="Casualties">cs</span></th>
EOQ;
if ($team->usesExp) {
    echo<<< EOQ
            <th><span title="Experience">exp</span></th>
EOQ;
} else {
    echo<<< EOQ
            <th><span title="Most Valuable Players">mvp</span></th>
EOQ;
}
echo<<< EOQ
            <th>spps</th>
            <th>Cost</th>
          </tr>

EOQ;
$i = 0;
$totalCmps = 0;
$totalTds = 0;
$totalInts = 0;
$totalCas = 0;
$totalMVPS = 0;
for ($i = 1; $i < 17; $i++) {
    $player = $team->getPlayer($i);
    echoPlayerRow($player, $i, !$player ? false : $team->race->getPosition($player->position), $team->usesExp);
    if ($player != false) {
        $totalCmps += $player->completions;
        $totalTds += $player->tds;
        $totalInts += $player->interceptions;
        $totalCas += $player->casualties;
        if ($team->usesExp) {
            $totalMVPS += $player->exps;
        } else {
            $totalMVPS += $player->mvps;
        }
    }
}
echo<<< EOQ
          <tr>
            <td colSpan="7">&nbsp;</td>
            <td colSpan="2" align="center"><font size="-1">Totals</font></td>
            <td align="center"><font size="-1">$totalCmps</font></td>
            <td align="center"><font size="-1">$totalTds</font></td>
            <td align="center"><font size="-1">$totalInts</font></td>
            <td align="center"><font size="-1">$totalCas</font></td>
            <td align="center"><font size="-1">$totalMVPS</font></td>
            <td align="center"><font size="-1">
EOQ;
echo $team->getSPPsTotal() . "</font></td>\n<td align=\"right\"><font size=\"-1\">" . $team->getPlayerCostTotal() . "k</font></td>\n</tr>\n";
$rerollCost = $team->rerolls * $team->race->rerollCost;
$ffCost = $team->fanFactor * 10;
$coachCost = $team->coaches * 10;
$coachName = $team->coach->getName();
$cheerCost = $team->cheerleaders * 10;
$apocCost = $team->apothecary * 50;
$tr = $team->getTR();
$totalCost = $team->getTotalCost();
$race = $team->race;
$raceDescription = $race->getRaceDisplay();
echo<<< EOQ
          <tr align="center">
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Race:</td>
            <td colSpan=5><a href="race_viewer.php?race_id=$race->id">$raceDescription</a></td>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Re-Rolls:</td>
            <td colSpan="2">$team->rerolls</td>
            <td style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="4">x $race->rerollCost k =</td>
            <td align="right">$rerollCost k</td></tr>
          <tr align="middle">
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Coach:</td>
            <td colSpan=5>$coachName</td>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Fan Factor:</td>
            <td colSpan="2">$team->fanFactor</td>
            <td style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="4">x 10 k =</td>
            <td align="right">$ffCost k</td></tr>
          <tr align=middle>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Treasury:</td>
            <td colSpan=5>$team->treasury</td>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Assistant Coaches:</td>
            <td colSpan="2">$team->coaches</td>
            <td style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="4">x 10 k =</td>
            <td align="right">$coachCost k</td></tr>
          <tr align=middle>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Money Lent:</td>
            <td colSpan=5>$team->moneyLent</td>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Cheerleaders:</td>
            <td colSpan="2">$team->cheerleaders</td>
            <td style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="4">x 10 k =</td>
            <td align="right">$cheerCost k</td></tr>
          <tr align=middle>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Money Borrowed:</td>
            <td colSpan=5>$team->moneyBorrowed</td>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Apothecary:</td>
            <td colSpan="2">$team->apothecary</td>
            <td style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="4">x 50 k =</td>
            <td align="right">$apocCost k</td></tr>
          <tr align=middle>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Last Update:</td>
            <td colSpan=5>$team->lastUpdate</td>
            <td align="right" rowspan="2" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan=8>TOTAL COST OF TEAM:</td>
            <td align="right" rowspan="2">$totalCost k</td>
          <tr align=middle>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Team Value:</td>
            <td colSpan=5><b>$tr</b> k</td>
          </tr>
          </tbody>
        </table>
        <table border="0"><tr><td>
        <div align="left" class="small">
EOQ;
$canEdit = isset($loggedin)  && $loggedin->id == $team->coach->id; 
	if ($canEdit) {
echo<<< EOQ
<div id="EditComment" style="display: none">
  <table width="100%" cellspacing="5">
    <tr>
	  <form method="post">
      <td class="small" align="center">
      <input type="submit" name="submitComment" value="Update">
      <br /><br /><a href="javascript:comment()">[Cancel]</a> 
      </td>
      <td align="left"><TEXTAREA name="comments" rows="5" cols="100">$team->comments</TEXTAREA></td>
      </form>
    </tr>
  </table>
</div>

<script language=javascript type='text/javascript'>
function edit() {
	changeDivDisplay('EditComment', 'block');
	changeDivDisplay('ShowComment', 'none');
}

function comment() {
	changeDivDisplay('EditComment', 'none');
	changeDivDisplay('ShowComment', 'block');
}
</script> 
EOQ;
	}
echo<<< EOQ
	<div id="ShowComment" style="display: block; FONT-SIZE: 90%;" class="wrap">
EOQ;
	
    if ($canEdit) {
        echo<<< EOQ
		<a href="javascript:edit()" class="sup">[Edit]</a>&nbsp;
EOQ;
    }
    echo<<< EOQ
<pre class="wrap">$team->comments</pre></div></div>

</td></tr></table>
EOQ;
if (hasFixturesTeam($team)) {
echo<<< EOQ
        <h2>Upcoming Fixtures</h2>
        <table border="1" borderColorLight=$team->borderLight borderColorDark=$team->borderDark cellpadding="5" cellspacing="0">
          <tr align=middle style="background-color: $team->titleBackground; color: $team->titleText;">
            <th>Season</th>
            <th>Round</th>
            <th>Opponent</th>
            <th>Team</th>
            <th>Race</th>
            <th>TV</th>
          </tr>
EOQ;
    $fixtures = loadFixturesTeam($team);
	$frd = new FixtureRoundDao();
	$i = 0;
    foreach ($fixtures as $fixture) {
		$rounds = $frd->findFixtureRounds($fixture->season);
        echoTeamFixtureRow($fixture, $rounds, $i, $id);
    }
echo<<< EOQ
        </table>
EOQ;
}
echo<<< EOQ
        <h2>Match Record</h2>
        <table border="1" borderColorLight=$team->borderLight borderColorDark=$team->borderDark cellpadding="5" cellspacing="0">
          <tr align=middle style="background-color: $team->titleBackground; color: $team->titleText;">
            <th rowSpan=2>Result</th>
            <th rowSpan=2>Opponent</th>
            <th rowSpan=2>Team</th>
            <th colSpan="2">Score</th>
            <th colSpan="2">Cas</th>
            <th rowSpan=2>Winnings</th>
            <th rowSpan=2>Fan Factor</th>
            <th rowSpan=2>&nbsp;</th>
          </tr>
          <tr align=middle style="background-color: $team->titleBackground; color: $team->titleText;">
            <th>For</th>
            <th>Ag</th>
            <th>For</th>
            <th>Ag</th>
          </tr>
EOQ;
$md = new MatchDao();
$teamMatches = $md->findTeamMatches($team->id);
//$teamMatches = array_reverse($teamMatches, true);
foreach ($teamMatches as $match) {
    if ($match->homeRec->teamId == $team->id) {
        echoMatch($match->homeRec, $match->awayRec, $match);
    } else
        echoMatch($match->awayRec, $match->homeRec, $match);
}
echo<<< EOQ
        </table>
    </center>
    </font>
  </body>
</html>
EOQ;
?>